<?

$srcPchOdrHdr     = "PCH_ORDER_HDR";
$srcPchOdrDtl     = "PCH_ORDER_SUB_DTL";
$srcDbCategory    = "MST_MODEL_CAT";
$srcDbGroup       = "MST_MODEL_GRP";
$srcDbConvertion  = "MST_MODEL_UNIT";

$CHtml = new CGHtml;

/**
* We extract the passed string and jobId
*
*/
$tmpRecvQty = 0;
$txNumber   = array();
                  
$maxJobId = sizeof($_SESSION["passedJobId"]);

if($maxJobId > 0 || 
   $_SESSION["modify_mode"]){
  
  /**
  * Check if we just firstly called
  * YES: We proccess all passed variables
  * NO : Continue with previous value
  *
  */
  if($_POST["cat_pick"]){
  
    for($iJid = 0; $iJid < $maxJobId; $iJid++){
      
      $idxTx    = sizeof($txNumber);
      
      $txNumber[$idxTx] = explode(",", 
                                  $_SESSION["passedString"][$iJid]);
    }
  }

  if($COracle->connect()){
  
    /**
    * SubDetails table
    *
    */
    echo  "<table border='1' class='dedeq' >\n".
          "<th> NO      </th>\n".
          "<th> Drop    </th>\n".
          "<th> Odr-Date</th>\n".
          "<th> Odr-REF </th>\n".
          "<th> Odr-NO  </th>\n".
          "<th> Cat     </th>\n".
          "<th> Grp     </th>\n".
          "<th> Model   </th>\n".
          "<th> Unit    </th>\n".
          "<th> Qty     </th>\n".
          "<th> Cvt     </th>\n".
          "<th> Qty     </th>\n".
          "<th> Received</th>\n".
          "<th> Price   </th>\n".
          "<th> #       </th>\n";
    
    /**
    * And now we fetch the data from DB
    * foreach jobId's txId and also
    * check if we are firstly called
    * if not addTx or not dropRow
    * or not back or not modify
    *
    */
    if(!$_POST["entry"]   &&
       !$_POST["addTx"]   &&
       !$_POST["droprow"] &&
       !$_POST["back"]    && 
       !$_POST["modify"]){
    
      for($iJid = 0; $iJid < $maxJobId; $iJid++){
        
        fetchTxHeader($COracle,
                      $_SESSION["passedJobId"][$iJid],
                      $srcPchOdrHdr,
                      $resHeader);
        
        $maxTxId = sizeof($txNumber[$iJid]);
        
        for($iTx = 0; $iTx < $maxTxId; $iTx++){
          
          fetchTxDetail($COracle,
                        $_SESSION["passedJobId"][$iJid],
                        $txNumber[$iJid][$iTx],
                        $srcPchOdrDtl,
                        $resDetail);
          
          fetchCategory($COracle,
                        $resDetail["ICAT"][0],
                        $srcDbCategory,
                        $resCategory);
          
          fetchGroup($COracle,
                     $resDetail["IGROUP"][0],
                     $srcDbGroup,
                     $resGroup);
          
          $propListConvertion = $CJs->jsOnChange("initConvertion(\"cvtEnt[".$_SESSION["init_num"]."]\",
                                                                 \"lstCvtEnt[".$_SESSION["init_num"]."]\",
                                                                 \"recvQtyEnt[".$_SESSION["init_num"]."]\");");
          
          $cvtName = "lstCvtEnt[".$_SESSION["init_num"]."] ";
          
          initListConvertion($COracle,
                             $cvtName,
                             $_SESSION["curLevelD"],
                             $resDetail["CVT_TO"][0],
                             $resDetail["UNIT"][0],
                             $propListConvertion,
                             $srcDbConvertion,
                             $listCvtContent);
                            
          $calcedRecvQuantity = $resDetail["QTY"][0] - $resDetail["RECV_QTY"][0];
                        
          echo  "<tr>\n".
          
                  "<td> ".($_SESSION["init_num"]+1)." </td>\n".
          
                  "<td> ".$CHtml->formCheckBox("dropBox[]",
                                               ($_SESSION["init_num"] + 1),
                                               $propCheckers)." </td>\n".
                  
                  "<td> ".$CHtml->formInput("odrDateEnt[".$_SESSION["init_num"]."]",
                                            date("d-m-Y", strtotime($resHeader["TR_DATE"][0])),
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                  
                  "<td> ".$CHtml->formInput("odrRefEnt[".$_SESSION["init_num"]."]",
                                            $_SESSION["passedJobId"][$iJid],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                  
                  "<td> ".$CHtml->formInput("odrNoEnt[".$_SESSION["init_num"]."]",
                                            $txNumber[$iJid][$iTx],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                  
                  "<td> ".$CHtml->formInput("catEnt[".$_SESSION["init_num"]."]",
                                            $resCategory,
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  "<td> ".$CHtml->formInput("grpEnt[".$_SESSION["init_num"]."]",
                                            $resGroup,
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                                                                                                          
                  "<td> ".$CHtml->formInput("modelEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["MODEL"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  $CHtml->formHidden("modelIdEnt[".$_SESSION["init_num"]."]",
                                     $resDetail["MODEL_ID"][0],
                                     _MODE_READONLY).
                                            
                  "<td> ".$CHtml->formInput("unitEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["UNIT"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  "<td> ".$CHtml->formInput("qtyEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["QTY"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  "<td> ".$CHtml->formInput("cvtEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["CVT_TO"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  "<td> ".$CHtml->formInput("cvtQtyEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["CVT_QTY"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  "<td>   <table class='dedeq'>
                          <tr>
                          
                            <td> ".$listCvtContent." </td>
                      
                            <td> ".$CHtml->formInput("recvQtyEnt[".$_SESSION["init_num"]."]",
                                                     $calcedRecvQuantity,
                                                     $numStyleSz5.
                                                     $propReceived)." </td> 
                          </tr>
                          </table>
                        </td>\n".
                                                  
                  "<td> ".$CHtml->formInput("priceEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["PRICE"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                                            
                  "<td> ".$CHtml->formInput("hrzTotEnt[".$_SESSION["init_num"]."]",
                                            $resDetail["HRZ_TOT"][0],
                                            $numStyleSz5.
                                            _MODE_READONLY)." </td>\n".
                "</tr>\n";
          
          $tmpRecvQty += (int)$calcedRecvQuantity;
                
          $_SESSION["init_num"]++;      
        }
      }
    }else{
      
      for($iTx = 0; $iTx < $_SESSION["init_num"] ; $iTx++){
      
        $propListConvertion = $CJs->jsOnChange("initConvertion(\"cvtEnt[".$iTx."]\",
                                                               \"lstCvtEnt[".$iTx."]\",
                                                               \"recvQtyEnt[".$iTx."]\");");
        
        $cvtName = "lstCvtEnt[".$iTx."] ";
        
        initListConvertion($COracle,
                           $cvtName,
                           $_SESSION["curLevelD"],
                           $_POST["lstCvtEnt"][$iTx],
                           $_POST["unitEnt"][$iTx],
                           $propListConvertion,
                           $srcDbConvertion,
                           $listCvtContent);
         
        echo  "<tr>\n".
            
                "<td> ".($iTx + 1)." </td>\n".
        
                "<td> ".$CHtml->formCheckBox("dropBox[]",
                                             ($iTx + 1),
                                             $propCheckers)." </td>\n".
                
                "<td> ".$CHtml->formInput("odrDateEnt[".$iTx."]",
                                          $_POST["odrDateEnt"][$iTx],
                                          $numStyleSz5. 
                                          _MODE_READONLY)." </td>\n".
                
                "<td> ".$CHtml->formInput("odrRefEnt[".$iTx."]",
                                          $_POST["odrRefEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                
                "<td> ".$CHtml->formInput("odrNoEnt[".$iTx."]",
                                          $_POST["odrNoEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                
                "<td> ".$CHtml->formInput("catEnt[".$iTx."]",
                                          $_POST["catEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                          
                "<td> ".$CHtml->formInput("grpEnt[".$iTx."]",
                                          $_POST["grpEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                                                                                                        
                "<td> ".$CHtml->formInput("modelEnt[".$iTx."]",
                                          $_POST["modelEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                
                $CHtml->formHidden("modelIdEnt[".$iTx."]",
                                   $_POST["modelIdEnt"][$iTx],
                                   _MODE_READONLY).
                                                               
                "<td> ".$CHtml->formInput("unitEnt[".$iTx."]",
                                          $_POST["unitEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                          
                "<td> ".$CHtml->formInput("qtyEnt[".$iTx."]",
                                          $_POST["qtyEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                          
                "<td> ".$CHtml->formInput("cvtEnt[".$iTx."]",
                                          $_POST["cvtEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                          
                "<td> ".$CHtml->formInput("cvtQtyEnt[".$iTx."]",
                                          $_POST["cvtQtyEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                          
                "<td> <table class='dedeq'>
                      <tr>
                      
                        <td> ".$listCvtContent." </td>
                  
                        <td> ".$CHtml->formInput("recvQtyEnt[".$iTx."]",
                                                 $_POST["recvQtyEnt"][$iTx],
                                                 $numStyleSz5.
                                                 $propReceived)." </td> 
                      </tr>
                      </table>
                    </td>\n".
                                            
                "<td> ".$CHtml->formInput("priceEnt[".$iTx."]",
                                          $_POST["priceEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
                                          
                "<td> ".$CHtml->formInput("hrzTotEnt[".$iTx."]",
                                          $_POST["hrzTotEnt"][$iTx],
                                          $numStyleSz5.
                                          _MODE_READONLY)." </td>\n".
              "</tr>\n";
        
        $tmpRecvQty += (int)$_POST["recvQtyEnt"][$iTx];
      }
    }
    
    /**
    * Print the total
    *
    */
    $_POST["totRecvQtyEnt"] = $tmpRecvQty;
    
    echo  "<tr>\n".
            
            "<td colspan='12' align='right'><b> TOTAL </b></td>\n".
            
            "<td> ".$CHtml->formInput("totRecvQtyEnt",
                                      $_POST["totRecvQtyEnt"],
                                      $propTotRecvQuantity.
                                      $fullSpaceWNumStyle)." </td>\n".
            
            "<td colspan='2'> </td>\n".
            
          "</tr>\n";
          
    echo  "</table>\n".
          "<br/>\n";
    
    
    echo  $CGHtml->formInputButton( "Entry-data",
                                    "entry",
                                    "Entry-data",
                                    $stlButton).
    			/*$CGHtml->formInputButton( "Add-row",
                                    "addrow",
                                    "Add-row",
                                    $stlButton).* No need of add row */
          $CGHtml->formInputButton( "Add-Tx",
                                    "addTx",
                                    "Add-Tx",
                                    $stlButton).
          $CGHtml->formInputButton( "Drop-row",
                                    "droprow",
                                    "Drop-row",
                                    $stlButton).
        	"</form>\n ".
        	"</body>\n";
        	
    $COracle->dc();
  }else{
    
    usrDie("Oracle Connection Failed");
  }
}else{
  
  usrDie("No Input Available");
}
?>